// pages/me/notice/notice.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo:{},
        noticeList:{}, // 对象 页数为key，防止多次触发触底造成数据重复现象
        searchParams:{
          pageNum:1,
          pageSize:6,//默认为6
          total:0, //总条数
          pages: 0, //总页数
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    doNothing(){},
    onLoad: function (options) {
        this.InitUserStatus();
    },
    // 检查一下登录状态。
    async InitUserStatus(){
        app.showLoading("获取用户信息");
        await app.checkToken();
        this.InitUserInfo();
        wx.hideLoading();
    },
    InitUserInfo(){
        let userInfo = app.globalData.userInfo;
        this.setData({
            'userInfo':userInfo
        });
        this.goQuery();
    },
    goQuery(){
      this.setData({
        'searchParams.pageNum':1,
        'searchParams.total':'',
        'searchParams.pages':'',
        'noticeList': {}
      });
      this.noticeList(this.data.searchParams);
    },
    noticeList(searchParams){
      // 整理完成进行搜索操作。
      app.sHttp.noticeList(searchParams).then(res=>{
          let noticeList = this.data.noticeList;
          res.result.noticeList.forEach(item =>{
              if (0 == item.isReaded) {
                  this.noticeRead(item.noticeId);
              }
          })
          noticeList[searchParams.pageNum] = res.result.noticeList;
          if (0 == res.result.total) {
            this.setData({
              'noData': true
            })
          }else{
              this.setData({
                  'noData': false,
                  'noticeList': noticeList,
                  'searchParams.total':res.result.total,
                  'searchParams.pages': res.result.pages
                })
          }
        },err=>{
          app.error(err.msg);
        })
    },
    onReachBottom(){
        if (this.data.searchParams.pageNum == this.data.searchParams.pages) {
            //app.alert('没有更多了哦~');
        }else{
            let self = this;
            this.setData({
                'searchParams.pageNum': self.data.searchParams.pageNum+1
            });
            this.noticeList(this.data.searchParams);
        }
    },
    noticeRead(noticeId){
      app.sHttp.noticeRead(noticeId).then(res=>{
      },err=>{
        app.error(err.msg);
      })
    },
    // 跳转到对应的模块
    goNoticeMsg(e){
        let pageNum = e.currentTarget.dataset.num;
        let index = e.currentTarget.dataset.idx;
        let noticeItem = this.data.noticeList[pageNum][index];
        switch (noticeItem.noticeType) {
            case 'carAuditNoPass': //车辆审核不通过--跳转到修改车辆
                this.carManageDetail(noticeItem.objId);
                break;
            case 'wantBuyAuditNoPass': //求购审核不通过--跳转到发布页面，带上之前的信息重新发布求购
                this.editWantBuy(noticeItem.objId);
                break;
            case 'auctionAuditPass': //拍卖审核通过--跳转到拍卖详情
                this.auctionAuditPass(noticeItem.objId);
                break;
            case 'auctionAuditNoPass':  //拍卖审核不通过--跳转到拍卖发布页面，带上之前的信息重新发布拍卖
                // this.auctionAuditNoPass(noticeItem.objId);
                break;
            case 'secondBatchAuditNoPass': //一口价审核不通过--跳转到一口价发布页面，带上之前的信息重新发布一口价
                
                break;
            case 'carOfferPrice': //车辆出价--跳转到出价列有
                this.carOfferPrice();
                break;
            default:
                break;
        }
    },
    carManageDetail(carId){
      wx.navigateTo({
        url: '/pages/garage/carDetail/carDetail?cid=' + carId,
      })
    },
    auctionAuditPass(aid){
      wx.navigateTo({
        url: '/pages/card/auction/auction?aid=' + aid,
      })
    },
    carOfferPrice(){
      wx.navigateTo({
        url: '/pages/me/offerPrice/offerPrice',
      })
    },
    editWantBuy(buyId){
        wx.navigateTo({
          url: '/pages/me/wantBuy/info/info?bid=' + buyId
        })
    },
    auctionAuditNoPass(aid){
      wx.navigateTo({
        url: '/pages/release/auction/auction?aid=' + aid
      })
    },
})